<template>
  <div class="shop-container">
      <van-nav-bar title="购物车"> </van-nav-bar>

    <div class="edit-box">
      <van-button
        icon="edit"
        style="border: none"
        size="mini"
        :type="isEdit ? 'danger' : 'default'"
        @click="editClick"
      >
        编辑
      </van-button>
    </div>

    <div class="goods-container">
      <div class="goods-box" v-for="(item, index) in shopList" :key="index">
        <div class="checked-box">
          <van-checkbox
            v-show="!isEdit"
            @change="itemCheckedChange"
            v-model="item.checked"
          ></van-checkbox>
          <van-button
            v-show="isEdit"
            @click="deleteGoods(index)"
            icon="minus"
            type="danger"
            size="mini"
          ></van-button>
        </div>

        <div class="img-box">
          <img :src="item.picUrl" style="width: 100%; height: 100%" alt="" />
        </div>

        <div class="info-box">
          <div class="goods-name">{{ item.name}}</div>
          <div class="price-box">
            <span>￥{{ item.price }}</span>
            <van-stepper
              input-width="20px"
              button-size="20px"
              v-model="item.count"
              @change="getTotal"
            />
          </div>
          <div class="date">
            <span>{{ item.createDate }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="footer-box">
      <div class="all-checked">
        <van-checkbox v-model="allChecked" @change="allCheckedChange"
          >全选</van-checkbox
        >
      </div>
      <div class="total">
        <span style="font-size: 14px">总计</span>
        <span style="font-size: 16px; color: red">￥{{ total }}</span>
      </div>
      <div class="total-btn" @click="cartindex">结算({{ allCount }})</div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { Toast } from 'vant';
export default {
  data() {
    return {
      isEdit: false,
      allChecked: false,
      total: 0,
      allCount: 0,
      shopList: [],
      allCheckedGoods: []
    };
  },
  mounted() {
    this.getData()
  },
  methods: {
    //购物车列表
    getData() {
      axios({
        url: "http://127.0.0.1:3000/shop/list",
        method: "get",
      }).then((res) => {
        this.shopList = res.data;
        console.log('11111', this.shopList);
      });
    },
    editClick() {
      this.isEdit = !this.isEdit;
    },
    deleteGoods() {
    axios({
        url: "http://127.0.0.1:3000/index/delete",
        method: "get",
        params: {
          ...this.shopList 
        },
      }).then((res) => {
        Toast.success('删除成功')
        this.getData();
      });
    },
    cartindex() {
      this.$router.push({
        path: "/pay",
        query: {
          allCheckedGoodsList: this.allCheckedGoods,
      
        }
      });
    },
    // 从shopList中去过滤checked为true的对象，拿到数组的length
    itemCheckedChange() {
      let filterGoods = this.shopList.filter((item) => {
        return item.checked === true;
      });

      this.allCount = filterGoods.length;

      this.allCheckedGoods = filterGoods

      this.getTotal();
    },
    // 循环shopList数组，将每个对象的goodsPrice属性加起来
    getTotal() {
      let filterGodds = this.shopList.filter((item) => {
        return item.checked === true;
      });

      // 每次循环过后，prev接收上一次return的值作为，下一次的prev

      this.total = filterGodds.reduce((prev, next) => {
        console.log(prev);
        console.log(next);
        return Number(prev) + (Number(next.price) * Number(next.count));
      }, 0);
    },
    allCheckedChange(value) {
      this.shopList = this.shopList.map((item) => {
        return {
          ...item,
          checked: value,
        };
      });
      console.log('aaaaaa',value)
    },
  },
};
</script>

<style lang="scss" scoped>
.shop-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #fafafa;

  .goods-container {
    flex: 1;
    overflow: auto;
  }
}
.header {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #ddd;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 10px;
  box-sizing: border-box;
  background: #fff;
}

.edit-box {
  // display: flex;
  // justify-content: flex-end;
  text-align: right;
  padding-right: 10px;
  box-sizing: border-box;
}

.goods-box {
  margin-bottom: 10px;
  display: flex;
  height: 100px;

  .checked-box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50px;
    height: 100%;
  }

  .img-box {
    width: 100px;
    height: 100%;
  }

  .info-box {
    display: flex;
    flex-direction: column;
    flex: 1;
    height: 100%;
    padding-left: 10px;

    .goods-name {
      font-size: 14px;
    }

    .goods-tags {
      font-size: 14px;
      color: #aaa;

      span {
        border: 1px solid #ddd;
        margin-right: 10px;
        padding: 2px 4px;
      }
    }

    .price-box {
      display: flex;
      justify-content: space-between;
      padding-right: 20px;
      color: red;
    }

    .date {
      display: flex;
      align-items: flex-end;
      justify-content: flex-end;
      flex: 1;
      font-size: 14px;
      color: #aaa;
      text-align: right;
      padding-right: 20px;
    }
  }
}

.footer-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 50px;
  padding-left: 15px;
  box-sizing: border-box;

  .total-btn {
    width: 110px;
    height: 50px;
    background: red;
    line-height: 50px;
    text-align: center;
    color: #fff;
  }
}
</style>